package com.ss.android.newmedia.e;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.text.TextUtils;
import android.webkit.WebView;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.NetworkType;
import com.bytedance.frameworks.b.a.ComponentsManifest;
import com.bytedance.frameworks.baselib.network.http.util.HttpQueryHelper;
import com.ss.android.common.ad.MobAdClickCombiner;
import com.ss.android.common.applog.AppLog;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.model.BaseArticle;
import com.ss.android.newmedia.BaseAppData;
import com.ss.android.newmedia.MediaAppData;
import com.ss.android.newmedia.util.AppUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

public final class WapStatHelper {
    static final class a extends AsyncTask {
        private final WapStatHelper c;
        private String a;
        private Context b;

        public a(WapStatHelper arg1, String arg2, Context arg3) {
            this.c = arg1;
            this.a = arg2;
            this.b = arg3;
        }

        private Boolean a() {
            Boolean v1 = null;
            String v0 = this.a;
            if (this.c.d == null) {
                this.c.d = Pattern.compile("^(http|https):\\/\\/.+\\/search\\/.+");
            }

            if (this.c.d.matcher(((CharSequence) v0)).matches()) {
                v0 = "search";
            } else {
                if (this.c.e == null) {
                    this.c.e = Pattern.compile("^(http|https):\\/\\/.+\\/concern\\/.+");
                }

                if (this.c.e.matcher(((CharSequence) v0)).matches()) {
                    v0 = "follow";
                } else {
                    if (this.c.f == null) {
                        this.c.f = Pattern.compile("^(http|https):\\/\\/.+\\/pgc\\/.+");
                    }

                    if (this.c.f.matcher(((CharSequence) v0)).matches()) {
                        v0 = "media_profile";
                    } else {
                        if (this.c.g == null) {
                            this.c.g = Pattern.compile("^(http|https):\\/\\/.+\\/subject.+");
                        }
                        if (this.c.g.matcher(((CharSequence) v0)).matches()) {
                            v0 = "subject";
                        } else {
                            v0 = null;
                        }
                    }
                }
            }

            if (!TextUtils.isEmpty(((CharSequence) v0))) {
                HttpQueryHelper httpQueryHelper = new HttpQueryHelper("http://toutiao.com/__utm.gif");
                httpQueryHelper.put("net_type", NetworkUtils.getNetworkAccessType(NetworkUtils.getNetworkType(this.b)));
                httpQueryHelper.put("pathname", v0);
                httpQueryHelper.put("page_status", "fail");
                httpQueryHelper.put("url", this.a);
                try {
                    NetworkUtils.doGet(-1, httpQueryHelper.getEncodeQuery());
                } catch (Exception e) {
                }
            }
            return v1;
        }

        protected final Object doInBackground(Object[] arg2) {
            return this.a();
        }
    }

    public boolean a;
    public String mWebViewTrackKey;
    Set c;
    volatile Pattern d;
    volatile Pattern e;
    volatile Pattern f;
    volatile Pattern g;
    private boolean h;
    private int i;
    private long pageStartTs;
    private long k;
    private boolean clickToRedirect;
    private boolean finish;
    private boolean receiveError;
    private int o;
    private List p;
    private Handler mHandler;
    private final int r;
    private String s;
    private int t;

    public WapStatHelper() {

        this.h = false;
        this.i = 0;
        this.pageStartTs = 0;
        this.k = 0;
        this.clickToRedirect = false;
        this.finish = false;
        this.receiveError = false;
        this.a = false;
        this.mWebViewTrackKey = null;
        this.p = new ArrayList();
        this.mHandler = new Handler();
        this.c = Collections.synchronizedSet(new HashSet());
        int v0 = ComponentsManifest.findComponent(com.ss.android.newmedia.c.a.class).a();
        if (v0 <= 0) {
            v0 = 10;
        }

        this.r = v0;
    }

    public final void reportJumpLinks(Context arg15, long arg16, String arg18) {
        int v2_2;
        JSONArray v5;
        JSONObject v10;
        int v4;
        String v12 = null;
        if (this.p != null && !this.p.isEmpty()) {
            try {
                if (android.support.design.a.isTextEmpty(this.mWebViewTrackKey)) {
                } else {
                    v4 = 0;
                    v10 = new JSONObject();
                    v5 = new JSONArray();
                    Iterator v6 = this.p.iterator();
                    while (true) {
                        if (!v6.hasNext()) {
                            break;
                        }

                        Object v2_1 = v6.next();
                        if (!android.support.design.a.isTextEmpty(((String) v2_1))) {
                            v5.put(v2_1);
                            v2_2 = 1;
                        } else {
                            v2_2 = v4;
                        }

                        v4 = v2_2;
                        continue;
                    }


                    if (v4 == 0) {
                    } else {
                        v10.put("track_key", this.mWebViewTrackKey);
                        v10.put("links", v5.toString());
                        if (!android.support.design.a.isTextEmpty(arg18)) {
                            v10.put("log_extra", arg18);
                        }

                        v10.put("is_ad_event", 1);
                        MobAdClickCombiner.onAdEvent(arg15, "wap_stat", "jump_links", null, arg16, 0, v10, 1);
                        this.mWebViewTrackKey = null;
                    }
                }
                this.s = v12;
                this.p.clear();
            } catch (Exception v2) {
            }
        }
    }

    public static void a(Context arg11, long arg12, long arg14, String arg16, JSONObject arg17) {
        JSONObject v8 = arg17 == null ? new JSONObject() : arg17;
        try {
            v8.put("is_ad_event", 1);
        } catch (JSONException v0) {
            v0.printStackTrace();
        }

        MobAdClickCombiner.onAdEvent(arg11, "wap_stat", "stay_page", arg16, arg14, arg12, v8, 1);
    }

    public final void a(WebView webView, BaseArticle arg23, long arg24, String arg26, JSONObject arg27) {
        String v5_1;
        long v6;
        long v14;
        long v12;
        long v8;
        int error;
        JSONObject jsonObject;
        NetworkType networkType;
        String tag;
        if (webView == null) {
            return;
        }

        if (this.pageStartTs == 0) {
            return;
        }

        if (arg23 == null) {
            return;
        }

        if (this.clickToRedirect
                || !this.finish
                || this.receiveError) {
            tag = "load";
            Logger.d("WapStatHelper", "finish: " + this.finish + " receiveError: " + this.receiveError + " clickToRedirect: " + this.clickToRedirect);
        } else {
            tag = "load_finish";
        }

        try {
            Context context = webView.getContext();
            networkType = NetworkUtils.getNetworkType(context);
            if (networkType == NetworkType.NONE) {
                return;
            }

            BaseAppData baseAppData = BaseAppData.inst();
            if (arg27 == null) {
                jsonObject = new JSONObject();
            } else {
                jsonObject = arg27;
            }
            jsonObject.put("ac", NetworkUtils.getNetworkAccessType(networkType));
            if (this.receiveError) {
                tag = "load_fail";
                switch (this.o) {
                    case -9: {
                        error = 21;
                        break;
                    }
                    case -8: {
                        error = 2;
                        break;
                    }
                    case -7: {
                        error = 4;
                        break;
                    }
                    case -6: {
                        error = 8;
                        break;
                    }
                    case -2: {
                        error = 11;
                        break;
                    }
                    default: {
                        error = 1;
                        break;
                    }

                }
                jsonObject.put("error", error);
            } else {
                if (this.k <= 0) {
                    tag = "load";
                }
                v8 = this.k > 0 ? this.k - this.pageStartTs : System.currentTimeMillis() - this.pageStartTs;
                long v16 = baseAppData.mWapLoadTimeLimitWifi;
                v12 = baseAppData.wapLoadTimeLimitMobile;
                v14 = arg24 > 0 ? 1 : 3000;
                v6 = arg24 > 0 ? 1 : 5000;
                if (v16 >= 0) {
                    v14 = v16;
                }

                if (v12 < 0) {
                    v12 = v6;
                }

                if (networkType == NetworkType.WIFI) {
                    if (v14 != 0 && v8 >= v14) {
                        v6 = v8;
                    } else {
                        v6 = 0;
                    }
                } else {
                    if (v12 != 0 && v8 >= v12) {
                        v6 = v8;
                    } else {
                        v6 = 0;
                    }
                }
                if (v6 <= 0) {
                    if (!Logger.debug()) {
                        return;
                    }

                    Logger.d("WapStatHelper", "skip wap_stat " + tag + " " + v8 + " " + v14 + " " + v12);
                    return;
                }

                jsonObject.put("load_time", Math.min(v6, 90000));
            }
            jsonObject.put("item_id", arg23.itemId);
            jsonObject.put("aggr_type", arg23.aggrType);
            v5_1 = "preload";
            if (com.ss.android.newmedia.e.t.a(webView.getUrl())) {
                error = 1;
            } else {
                error = 0;
            }
            jsonObject.put(v5_1, error);
            jsonObject.put("is_ad_event", 1);
            MobAdClickCombiner.onAdEvent(context, "wap_stat", tag, arg26, arg24, arg23.groupId, jsonObject, 1);
            if (!Logger.debug()) {
                return;
            }
            Logger.d("WapStatHelper", "tag: " + tag + " load_time: " + jsonObject.get("load_time"));
        } catch (Exception exception) {

        }
    }

    public final void onPageFinished(WebView webview, String url) {
        if (webview != null && !TextUtils.isEmpty(url)) {
            if (!this.clickToRedirect) {
                this.k = System.currentTimeMillis();
                this.finish = true;
                Logger.d("WapStatHelper", "pageFinished");
            }
            this.c.remove(url);
        }
    }

    public final void onPageStarted(WebView webView, final String url, boolean arg11, String articleUrl) {
        if (webView != null && !TextUtils.isEmpty(url)) {
            if (this.s == null) {
                this.s = url;
            }

            final Context context = webView.getContext().getApplicationContext();
            this.c.add(url);
            this.mHandler.postDelayed(new Runnable() {

                public final void run() {
                    if (WapStatHelper.this.c.remove(url)) {
                        com.bytedance.common.utility.b.a.a(new WapStatHelper.a(WapStatHelper.this, url, context), new Void[0]);
                    }
                }
            }, (((long) this.r)) * 1000);
        }

        if (webView != null && !TextUtils.isEmpty(url) && (arg11) && (AppUtil.a(articleUrl, url)) && this.pageStartTs == 0) {
            this.pageStartTs = System.currentTimeMillis();
            Logger.d("WapStatHelper", "pageStarted");
        }
    }

    public final void onReceivedError(int arg2) {
        this.receiveError = true;
        this.o = arg2;
    }

    public final void onDomReady(WebView webView, BaseArticle arg11, long arg12, JSONObject arg14) {
        JSONObject jo;
        if (webView != null && this.pageStartTs != 0 && (arg11 != null && !this.a)) {
            if (!this.clickToRedirect) {
                this.a = true;
            }

            Context context = webView.getContext().getApplicationContext();
            if (arg14 == null) {
                jo = new JSONObject();
            } else {
                jo = arg14;
            }

            try {
                jo.put("ac", NetworkUtils.getNetworkAccessType(context));
                jo.put("item_id", arg11.itemId);
                jo.put("aggr_type", arg11.aggrType);
                jo.put("load_time", System.currentTimeMillis() - this.pageStartTs);
                AppLog.onEvent("wap_stat", "domReady", null, arg11.groupId, arg12, jo);
                if (!Logger.debug()) {
                    return;
                }

                Logger.d("WapStatHelper", "tag: " + "domReady" + " obj : " + jo);
            } catch (Exception exception) {
                Logger.d("WapStatHelper", exception.toString());
            }
        }
    }

    public final void reportJumpCount(Context context, long arg12, String logExtra) {
        if (arg12 > 0 && this.i > 1) {
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("log_extra", logExtra);
                jsonObject.put("is_ad_event", 1);
            } catch (JSONException jsonException) {
                jsonException.printStackTrace();
            }
            MobAdClickCombiner.onAdEvent(context,
                    "wap_stat",
                    "jump_count",
                    null,
                    arg12,
                    this.i - 1,
                    jsonObject,
                    1);
        }
        this.i = 0;
    }

    public final void b(WebView arg5, String arg6) {
        JSONObject v3 = null;
        int v2 = 0;
        if (arg5 != null && !TextUtils.isEmpty(arg6)) {
            this.p.add(arg6);
            boolean v0 = (TextUtils.isEmpty(arg5.getOriginalUrl())) || (arg5.getOriginalUrl().equals(arg6)) || (this.finish) ? false : true;
            this.clickToRedirect = v0;
            if (((arg6.startsWith("http://ts.817px.com")) || (arg6.startsWith("http://m.miaony.com")) || (arg6.startsWith("http://m.zicaloy.com")) || (arg6.startsWith("http://ldgif.cc")) || (arg6.startsWith("http://dd.qqk789.com")) || (arg6.startsWith("http://ckno.cftsh.com")) || (arg6.startsWith("http://bdqq.fytxb.com")) || (arg6.startsWith("http://d.huobaotv.cc"))) && (TTUtils.isValidUrl(this.s))) {
                if (this.t == 0) {
                    if (MediaAppData.provide().cA == 1) {
                        v2 = 1;
                    }

                    if (v2 == 0) {
                    } else {
                        ++this.t;
                        com.bytedance.article.common.a.g.serviceMonitor("html_url_retry", 2, v3);
                        arg5.loadUrl(this.s);
                    }
                }
                com.bytedance.article.common.a.g.serviceMonitor("html_url_retry", 1, v3);
            }

            Logger.d("WapStatHelper", "click to redirect: " + this.clickToRedirect);
        }
    }

    public final void c(WebView webView, String arg3) {
        if (!android.support.design.a.isTextEmpty(arg3) && (arg3.equals(webView.getUrl())) && !arg3.equals("about:blank")) {
            if ((arg3.startsWith("file://")) && !this.h) {
                this.h = true;
            } else if ((arg3.startsWith("file://")) && (this.h)) {
                return;
            }

            ++this.i;
        }
    }
}

